#pragma once

#include<iostream>

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#include<unistd.h>
#include<strings.h>

#define CONV(addr_ptr) ((struct sockaddr*)addr_ptr)
namespace Net_Work
{
    enum
    {
        Socket_Err = 1,
        Bind_Err,
        Listen_Err
    };

    const static int backlog = 5;
    const static int defaultsockfd = -1;

    class Socket
    {
    public:
        virtual ~Socket() {}
        // 纯虚函数
        virtual void CreateSocketOrDie() = 0;
        virtual void BindSocketOrDie(uint16_t port) = 0; // 绑定需要一个端口
        virtual void ListenSocketOrDie(int backlog) = 0;
        virtual int AcceptConnection(std::string *peerip, uint16_t *peerport) = 0;
        virtual bool ConnectServer(std::string &serverip, uint16_t serverport) = 0;
        virtual int GetSocket() = 0;
        virtual void SetSocketFd(int sockfd) = 0;
        virtual void CloseSockfd() = 0;
        virtual bool Recv(std::string*buffer,int size)=0;
        virtual void Send(std::string send_string)=0;
    public:
        void BuildListenSocketMethod(uint16_t port, int backlog)
        {
            CreateSocketOrDie();
            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }
        bool BuidConnectSocketMethod(std::string &serverip, uint16_t serverport)
        {
            CreateSocketOrDie();
            return ConnectServer(serverip, serverport);
        }
        void BuildNormalSocketMethod(int sockfd)
        {
            SetSocketFd(sockfd);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int sockfd = defaultsockfd)
            : _sockfd(sockfd)
        {
        }
        ~TcpSocket()
        {
        }
        void CreateSocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
                exit(Socket_Err);
        }
        void BindSocketOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_addr.s_addr = INADDR_ANY;
            local.sin_port = htons(port);
            int n = ::bind(_sockfd, CONV(&local), sizeof(local));
            if (n < 0)
                exit(Bind_Err);
        }
        void ListenSocketOrDie(int backlog) override
        {
            int m = ::listen(_sockfd, backlog);
            if (m < 0)
                exit(Listen_Err);
        }
        int AcceptConnection(std::string *peerip, uint16_t *peerport) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newsockfd = ::accept(_sockfd, CONV(&peer), &len);
            *peerip = inet_ntoa(peer.sin_addr);
            *peerport = ntohs(peer.sin_port);
            if (newsockfd < 0)
                return -1;
            return newsockfd;
        }
        bool ConnectServer(std::string &serverip, uint16_t serverport) override
        {
            struct sockaddr_in server;
            bzero(&server, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_addr.s_addr = inet_addr(serverip.c_str());
            server.sin_port = htons(serverport);
            int k = ::connect(_sockfd, CONV(&server), sizeof(server));
            if (k == 0)
                return true;
            else
                return false;
        }
        int GetSocket() override
        {
            return _sockfd;
        }
        void SetSocketFd(int sockfd) override
        {
            _sockfd = sockfd;
        }
        void CloseSockfd() override
        {
            if (_sockfd > defaultsockfd)
                ::close(_sockfd);
        }
        bool Recv(std::string*buffer,int size)override
        {
            char inbuffer[size];
            int n=recv(_sockfd,inbuffer,sizeof(inbuffer)-1,0);
            if(n>0)
            {
                inbuffer[n]=0;
                *buffer+=inbuffer;
                return true;
            }
            else return false;
        }
        void Send(std::string send_string)override
        {
            send(_sockfd,send_string.c_str(),send_string.size(),0);
        }
    private:
        int _sockfd;
    };
}